package com.chb.app.service.impl;

import cn.hutool.core.math.MathUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chb.app.mapper.*;
import com.chb.app.service.HealthTestService;
import com.chb.app.vo.TestResultVo;
import com.hos.entity.his.Patient;
import com.hos.entity.hos.*;
import com.hos.utils.IdGeneratorUtil;
import com.hos.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author ：WuBai-T
 * @date ：Created in 2024/1/2 20:01
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class HealthTestImpl implements HealthTestService {

    @Autowired
    private TestDetaliMapper testDetaliMapper;

    @Autowired
    private HealthTestResultMapper healthTestResultMapper;


    @Override
    public List<TestDetail> getTestDetailById(String htId) {
        QueryWrapper<TestDetail> wrapper=new QueryWrapper<>();
        wrapper.eq("ht_id",htId);
        List<TestDetail> testDetails = testDetaliMapper.selectList(wrapper);
        return testDetails;
    }

    @Autowired
    private HealthPatientResultItemMapper healthPatientResultItemMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private HealthPatientResultMapper healthPatientResultMapper;

    @Resource
    private PaientMapper paientMapper;
    @Override
    public TestResultVo getTestResult(List<TestDetail> testDetails) {
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        QueryWrapper<Patient> wrapper2=new QueryWrapper<>();
        wrapper2.eq("phone",phone);
        Patient patient = paientMapper.selectOne(wrapper2);
        int count=0;
        for (TestDetail testDetail : testDetails) {
            count=count+testDetail.getHtResult();
        }
        QueryWrapper<HealthTestResult> wrapper=new QueryWrapper<>();
        wrapper.eq("ht_id",testDetails.get(0).getHtId());
        HealthTestResult healthTestResult = healthTestResultMapper.selectOne(wrapper);
        String prId = IdGeneratorUtil.nextId("PR");
        TestResultVo testResuktMsg = getTestResuktMsg(count, healthTestResult);
        PatientResult patientResult=new PatientResult();
        patientResult.setPrId(prId);
        patientResult.setHtId(testDetails.get(0).getHtId());
        patientResult.setPatientId(patient.getPatientId());
        patientResult.setHtTime(LocalDateTime.now());
        patientResult.setHtResTitle(testResuktMsg.getTestResultTitle());
        patientResult.setHtResContent(testResuktMsg.getTestResult());
        int insert = healthPatientResultMapper.insert(patientResult);
        for (TestDetail testDetail : testDetails) {
            PatientResultItem patientResultItem=new PatientResultItem();
            String result = getResult(testDetail);
            if (StringUtils.isNotEmpty(result)){
                patientResultItem.setResult(result);
            }
            patientResultItem.setPrId(prId);
            patientResultItem.setPatientId(patient.getPatientId());
            patientResultItem.setQuestionId(testDetail.getId()+"");
            healthPatientResultItemMapper.insert(patientResultItem);
        }
        return testResuktMsg;
    }

    private TestResultVo getTestResuktMsg(Integer count,HealthTestResult healthTestResult){
        TestResultVo testResultVo=new TestResultVo();
        if (4>=count && count>=0){
            testResultVo.setTestResultTitle(healthTestResult.getATitle());
            testResultVo.setTestResult(healthTestResult.getResA());
        } else if (10>count && count>=4) {
            testResultVo.setTestResultTitle(healthTestResult.getBTitle());
            testResultVo.setTestResult(healthTestResult.getResB());
        } else if (20>count && count>=10) {
            testResultVo.setTestResultTitle(healthTestResult.getCTitle());
            testResultVo.setTestResult(healthTestResult.getResC());
        } else if (25>count && count>=20) {
            testResultVo.setTestResultTitle(healthTestResult.getDTitle());
            testResultVo.setTestResult(healthTestResult.getResD());
        } else if (30>=count && count>=25) {
            testResultVo.setTestResultTitle(healthTestResult.getETitle());
            testResultVo.setTestResult(healthTestResult.getResE());
        }
        return testResultVo;
    }
    private String getResult(TestDetail testDetail){
        String htType = testDetail.getHtType();
        Integer htResult = testDetail.getHtResult();
        if (htType.equals("1")){
            if (htResult==1){
                return testDetail.getQuestionA();
            }else if (htResult==2){
                return testDetail.getQuestionB();
            }else if (htResult==3){
                return testDetail.getQuestionC();
            }else if (htResult==4){
                return testDetail.getQuestionD();
            }

        } else if (htType.equals("2")) {
            if (htResult==0){
                return testDetail.getQuestionN();
            }else if (htResult==1){
                return testDetail.getQuestionY();
            }
        }
        return null;
    }
    @Autowired
    private HealthTestListMapper healthTestListMapper;
    @Override
    public List<TestList> getAllHealthTest() {
        List<TestList> testLists = healthTestListMapper.selectList(null);
        return testLists;
    }

    @Override
    public TestResultVo getTestResult02(List<TestDetail> testDetails) {
        TestResultVo testResultVo=new TestResultVo();
        String abcd = getABCD(testDetails);
        QueryWrapper<HealthTestResult> wrapper=new QueryWrapper<>();
        String htId = testDetails.get(0).getHtId();
        wrapper.eq("ht_id",htId);
        HealthTestResult healthTestResult = healthTestResultMapper.selectOne(wrapper);
        String prId = IdGeneratorUtil.nextId("PR");
        PatientResult patientResult=new PatientResult();
        patientResult.setPrId(prId);
        patientResult.setHtId(htId);
        String token = WebUtil.getToken();
        String phone = redisTemplate.opsForValue().get("user:" + token);
        QueryWrapper<Patient> wrapper2=new QueryWrapper<>();
        wrapper2.eq("phone",phone);
        Patient patient = paientMapper.selectOne(wrapper2);
        if (abcd.equals("A")){
            patientResult.setHtResTitle(healthTestResult.getATitle());
            patientResult.setHtResContent(healthTestResult.getResA());
        } else if (abcd.equals("B")) {
            patientResult.setHtResTitle(healthTestResult.getBTitle());
            patientResult.setHtResContent(healthTestResult.getResB());
        } else if (abcd.equals("C")) {
            patientResult.setHtResTitle(healthTestResult.getCTitle());
            patientResult.setHtResContent(healthTestResult.getResC());
        } else if (abcd.equals("D")) {
            patientResult.setHtResTitle(healthTestResult.getDTitle());
            patientResult.setHtResContent(healthTestResult.getResD());
        }
        testResultVo.setTestResultTitle(patientResult.getHtResTitle());
        testResultVo.setTestResult(patientResult.getHtResContent());
        patientResult.setHtTime(LocalDateTime.now());
        patientResult.setPatientId(patient.getPatientId());
        healthPatientResultMapper.insert(patientResult);
        for (TestDetail testDetail : testDetails) {
            PatientResultItem patientResultItem=new PatientResultItem();
            String result = getResult(testDetail);
            if (StringUtils.isNotEmpty(result)){
                patientResultItem.setResult(result);
            }
            patientResultItem.setPrId(prId);
            patientResultItem.setPatientId(patient.getPatientId());
            patientResultItem.setQuestionId(testDetail.getId()+"");
            healthPatientResultItemMapper.insert(patientResultItem);
        }
        return testResultVo;
    }

    private String getABCD(List<TestDetail> testDetails){
        Map<String,Integer> map=new HashMap<>();
        Map<String,Integer> lastMap=new HashMap<>();
        map.put("A",0);
        map.put("B",0);
        map.put("C",0);
        map.put("D",0);
        lastMap.put("A",0);
        lastMap.put("B",0);
        lastMap.put("C",0);
        lastMap.put("D",0);
        for (int i = 0; i < 30; i++) {
            TestDetail testDetail = testDetails.get(i);
            if (i>=0 && i<15){
                Integer htResult = testDetail.getHtResult();
                if (htResult==1){
                    map.put("A",map.get("A")+1);
                } else if (htResult==2) {
                    map.put("B",map.get("B")+1);
                }else if (htResult==3) {
                    map.put("C",map.get("C")+1);
                }else if (htResult==4) {
                    map.put("D",map.get("D")+1);
                }
            }
            if (i>=15 && i<30){
                Integer htResult = testDetail.getHtResult();
                if (htResult==1){
                    lastMap.put("A",map.get("A")+1);
                } else if (htResult==2) {
                    lastMap.put("B",map.get("B")+1);
                }else if (htResult==3) {
                    lastMap.put("C",map.get("C")+1);
                }else if (htResult==4) {
                    lastMap.put("D",map.get("D")+1);
                }
            }
        }
        Integer lastA = map.get("A");
        Integer lastB = map.get("B");
        Integer lastC = map.get("C");
        Integer lastD = map.get("D");
        Integer afterA = map.get("A");
        Integer afterB = map.get("B");
        Integer afterC = map.get("C");
        Integer afterD = map.get("D");
        Integer Red=lastA+afterD;
        Integer Blue=lastB+afterC;
        Integer Yellow=lastC+afterB;
        Integer Green=lastD+afterA;
        int asInt = Arrays.stream(new int[]{Red, Blue, Yellow, Green}).max().getAsInt();
        if (asInt==Red){
            return "A";
        } else if (asInt==Blue) {
            return "B";
        } else if (asInt==Yellow) {
            return "C";
        } else{
            return "D";
        }


    }


}